package com.license.common.xmlparser;

import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import com.license.common.util.Constants;

public class WriteExcel {
	private WritableCellFormat timesBoldUnderline;
	private WritableCellFormat times;
	private String inputFile;

	public void setOutputFile(String inputFile) {
		this.inputFile = inputFile;
	}

	public void writeTestForCSO(Map<Point, Long> data, String path) {
		File file = new File(path);
		WorkbookSettings wbSettings = new WorkbookSettings();

		wbSettings.setLocale(new Locale("en", "EN"));

		WritableWorkbook workbook;
		try {
			workbook = Workbook.createWorkbook(file, wbSettings);
			workbook.createSheet("Report", 0);
			WritableSheet excelSheet = workbook.getSheet(0);

			createLabelForCSO(excelSheet);
			createContentForCSO(excelSheet,data);

			workbook.write();
			workbook.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void createLabelForCSO(WritableSheet sheet) throws WriteException {
		// Lets create a times font
		WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
		// Define the cell format
		times = new WritableCellFormat(times10pt);
		// Lets automatically wrap the cells
		times.setWrap(true);

		// Create create a bold font with unterlines
		WritableFont times10ptBoldUnderline = new WritableFont(
				WritableFont.TIMES, 10, WritableFont.BOLD, false,
				UnderlineStyle.SINGLE);
		timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
		// Lets automatically wrap the cells
		timesBoldUnderline.setWrap(true);

		CellView cv = new CellView();
		cv.setFormat(times);
		cv.setFormat(timesBoldUnderline);
		cv.setAutosize(true);

		// Write a few headers
		addCaption(sheet, 0, 0, "Pareto fitness");
		addCaption(sheet, 1, 0, "Antibody Nr.");
		addCaption(sheet, 2, 0, "Iteration Nr.");
		addCaption(sheet, 3, 0, "Beta Value");
		addCaption(sheet, 4, 0, "Time in ms");

	}
	
	private void createContentForCSO(WritableSheet sheet,
			Map<Point, Long> data) throws WriteException,
			RowsExceededException {
		for (Point point : data.keySet()){
			int index = 0;
				addNumber(sheet, (int) point.getX()/10 , (int) point.getY()/10, data.get(point));
				index++;
			
		}
	}

	
	  public void write(Map<String,List<Double>> data, String method, String filePath) throws IOException, WriteException {
		  File file = null;
		  if(method.equals("pso")) {
		   file = new File(filePath);}
		  else {
			  file = new File(Constants.CLASSIFIER_EXCEL_RESULTS);  
		  }
	    WorkbookSettings wbSettings = new WorkbookSettings();

		wbSettings.setLocale(new Locale("en", "EN"));

	    WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
	    workbook.createSheet("Report", 0);
	    WritableSheet excelSheet = workbook.getSheet(0);
	    if(method.equals("pso")) {
	    createLabelForPSOTest(excelSheet);
	    createContentForPSOTest(excelSheet,data);
	    } else if(method.equals("naiveBayes")) {
	    	 createLabelForBayesTest(excelSheet);
	 	    createContentForBayesTest(excelSheet,data);
	    }

		workbook.write();
		workbook.close();
	}
	  
	  

	private void createLabelForPSOTest(WritableSheet sheet) throws WriteException {
		// Lets create a times font
		WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
		// Define the cell format
		times = new WritableCellFormat(times10pt);
		// Lets automatically wrap the cells
		times.setWrap(true);

		// Create create a bold font with unterlines
		WritableFont times10ptBoldUnderline = new WritableFont(
				WritableFont.TIMES, 10, WritableFont.BOLD, false,
				UnderlineStyle.SINGLE);
		timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
		// Lets automatically wrap the cells
		timesBoldUnderline.setWrap(true);

		CellView cv = new CellView();
		cv.setFormat(times);
		cv.setFormat(timesBoldUnderline);
		cv.setAutosize(true);

	    // Write a few headers
	    addCaption(sheet, 0, 0, "Pareto fitness");
	    addCaption(sheet, 1, 0, "Particle Nr.");
	    addCaption(sheet, 2, 0, "Iteration Nr.");
	    addCaption(sheet, 3, 0, "C1");
	    addCaption(sheet, 4, 0, "C2");
	    addCaption(sheet, 5, 0, "C3");
	//    addCaption(sheet, 6, 0, "Old SBP");
	    addCaption(sheet, 6, 0, "Kalory result");
	//    addCaption(sheet, 8, 0, "Desired SBP");
	    addCaption(sheet, 7, 0, "Time in s");
	    addCaption(sheet, 8, 0, "Desired calory");
	    addCaption(sheet, 9, 0, "Std dev");

	}
	  
	  private void createLabelForBayesTest(WritableSheet sheet)
		      throws WriteException {
		    // Lets create a times font
		    WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
		    // Define the cell format
		    times = new WritableCellFormat(times10pt);
		    // Lets automatically wrap the cells
		    times.setWrap(true);
		    // Create create a bold font with unterlines
		    WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false,
		        UnderlineStyle.SINGLE);
		    timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
		    // Lets automatically wrap the cells
		    timesBoldUnderline.setWrap(true);

		    CellView cv = new CellView();
		    cv.setFormat(times);
		    cv.setFormat(timesBoldUnderline);
		    cv.setAutosize(true);
		    
		    // Write a few headers
		    addCaption(sheet, 0, 0, "name");
		    addCaption(sheet, 1, 0, "sex");
		    addCaption(sheet, 2, 0, "age");
		    addCaption(sheet, 3, 0, "height");
		    addCaption(sheet, 4, 0, "weight");
		    addCaption(sheet, 5, 0, "diabetic");
		    addCaption(sheet, 6, 0, "familyCardHistory");
		    addCaption(sheet, 7, 0, "smoker");
		    addCaption(sheet, 8, 0, "sbp");
		    addCaption(sheet, 9, 0, "Healty Probability");
		    addCaption(sheet, 11, 0, "Std dev");
	  }

	private void createContentForPSOTest(WritableSheet sheet,
			Map<String, List<Double>> data) throws WriteException,
			RowsExceededException {
		List<Double> particleList = data.get("particleNumber");
		List<Double> iterationNumber = data.get("iterationNumber");
		List<Double> c1List = data.get("c1");
		List<Double> c2List = data.get("c2");
		List<Double> c3List = data.get("c3");
		List<Double> paretoFitness = data.get("paretoFitness");
		List<Double> timeList = data.get("time");
		//    List<Double> newSbp = data.get("newSbp");
		  //  List<Double> oldSbp = data.get("oldSpb");
		    List<Double> stdDev = data.get("Stddev");
		    List<Double> desiredCalory = data.get("desireCaloires");
		    List<Double> actualKalory = data.get("actualKalory");
		    DecimalFormat df = new DecimalFormat("##.##");
		    
		    for (int i = 0; i < paretoFitness.size(); i++) {
			      // First column
			      addNumber(sheet, 0, i+1, (Double)paretoFitness.get(i));
			    }
		for (int i = 0; i < particleList.size(); i++) {
			// First column
			addNumber(sheet, 1, i + 1, (Double) particleList.get(i));
		}
		for (int i = 0; i < iterationNumber.size(); i++) {
			// First column
			addNumber(sheet, 2, i + 1, (Double) iterationNumber.get(i));
		}
		for (int i = 0; i < c1List.size(); i++) {
			// First column
			addNumber(sheet, 3, i + 1, (Double) c1List.get(i));
		}
		for (int i = 0; i < c2List.size(); i++) {
			// First column
			addNumber(sheet, 4, i + 1, (Double) c2List.get(i));
		}
		for (int i = 0; i < c3List.size(); i++) {
			// First column
			addNumber(sheet, 5, i + 1, (Double) c3List.get(i));
		}
		for (int i = 0; i < paretoFitness.size(); i++) {
			// First column
			addNumber(sheet, 0, i + 1, (Double) paretoFitness.get(i));
		}
		for (int i = 0; i < timeList.size(); i++) {
			// First column
			addNumber(sheet, 10, i + 1, (Double) paretoFitness.get(i));
		}
		// Lets calculate the sum of it
		/*
		 * StringBuffer buf = new StringBuffer(); buf.append("SUM(A2:A10)");
		 * Formula f = new Formula(0, 10, buf.toString()); sheet.addCell(f); buf
		 * = new StringBuffer(); buf.append("SUM(B2:B10)"); f = new Formula(1,
		 * 10, buf.toString()); sheet.addCell(f);
		 */

		/*
		 * // Now a bit of text for (int i = 12; i < 20; i++) { // First column
		 * addLabel(sheet, 0, i, "Boring text " + i); // Second column
		 * addLabel(sheet, 1, i, "Another text"); }
		 */
	}
	  private void createContentForBayesTest(WritableSheet sheet,Map<String, List<Double>> data) throws WriteException,
      RowsExceededException {
    List<Double> sex = data.get("sex");
    List<Double> age = data.get("age");
    List<Double> height = data.get("height");
    List<Double> weigth = data.get("weight");
    List<Double> diabetic = data.get("diabetic");
    List<Double> familyCardHistory = data.get("familyCardHistory");
    List<Double> smoker = data.get("smoker");
    List<Double> sbp = data.get("sbp");
    List<Double> healtyProbability = data.get("healtyProbability");
  //  List<Double> stdDev = data.get("stdDev");
    DecimalFormat df = new DecimalFormat("##.##");
    
    for (int i = 0; i < sex.size(); i++) {
      // First column
      addNumber(sheet, 0, i+1, (Double)sex.get(i));
    }
    for (int i = 0; i < age.size(); i++) {
	      // First column
	      addNumber(sheet, 1, i+1, (Double)age.get(i));
	    }
    for (int i = 0; i < height.size(); i++) {
	      // First column
	      addNumber(sheet, 2, i+1, (Double)height.get(i));
	    }
    for (int i = 0; i < weigth.size(); i++) {
	      // First column
	      addNumber(sheet, 3, i+1, (Double)weigth.get(i));
	    }
    for (int i = 0; i < diabetic.size(); i++) {
	      // First column
	      addNumber(sheet, 4, i+1, (Double)diabetic.get(i));
	    }
    for (int i = 0; i < familyCardHistory.size(); i++) {
	      // First column
	      addNumber(sheet, 5, i+1, (Double)familyCardHistory.get(i));
	    }
    for (int i = 0; i < sbp.size(); i++) {
	      // First column
	      addNumber(sheet, 6, i+1, (Double)sbp.get(i));
	    }
  for (int i = 0; i < smoker.size(); i++) {
	      // First column
	      addNumber(sheet, 7, i+1, (Double)smoker.get(i));
	    }
    for (int i = 0; i < healtyProbability.size(); i++) {
	      // First column
	      addNumber(sheet, 8, i+1,Double.valueOf(df.format((Double)healtyProbability.get(i))));
	    }
/*    for (int i = 0; i < stdDev.size(); i++) {
	      // First column
	      addNumber(sheet, 9, i+1, Double.valueOf(df.format((Double)stdDev.get(i))));
	    }*/
	  }

	
	private void addCaption(WritableSheet sheet, int column, int row, String s)
			throws RowsExceededException, WriteException {
		Label label;
		label = new Label(column, row, s, timesBoldUnderline);
		sheet.addCell(label);
	}

	private void addNumber(WritableSheet sheet, int column, int row, Double val)
			throws WriteException, RowsExceededException {
		Number number;
		number = new Number(column, row, val, times);
		sheet.addCell(number);
	}
	
	private void addNumber(WritableSheet sheet, int column, int row, Long val)
			throws WriteException, RowsExceededException {
		Number number;
		number = new Number(column, row, val, times);
		sheet.addCell(number);
	}

	private void addLabel(WritableSheet sheet, int column, int row, String s)
			throws WriteException, RowsExceededException {
		Label label;
		label = new Label(column, row, s, times);
		sheet.addCell(label);
	}
}
